package com.kun.rental.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.kun.rental.entity.CarrentalInfo;
import com.kun.rental.entity.Maintain;
import com.kun.rental.entity.Violation;
import com.kun.rental.mapper.CarrentalInfoMapper;
import com.kun.rental.mapper.MaintainMapper;
import com.kun.rental.service.IMaintainService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author ikun
 * @since 2024-07-04
 */
@Service
public class MaintainServiceImpl extends ServiceImpl<MaintainMapper, Maintain> implements IMaintainService {


    @Autowired
    private MaintainMapper maintainMapper;

    @Autowired
    private CarrentalInfoMapper autoInfoMapper;


    /**
     * 分页查询
     *
     * @param page
     * @param maintain
     * @return
     */
    @Override
    public Page<Maintain> searchByPage(Page<Maintain> page, Maintain maintain) {
        QueryWrapper<Maintain> queryWrapper = new QueryWrapper<>();
        queryWrapper.like(ObjectUtil.isNotEmpty(maintain.getNum()),
                "num", maintain.getNum());
        queryWrapper.like(ObjectUtil.isNotEmpty(maintain.getItem()),
                "item", maintain.getItem());
        queryWrapper.like(ObjectUtil.isNotEmpty(maintain.getLocation()),
                "location", maintain.getLocation());
        queryWrapper.le(ObjectUtil.isNotEmpty(maintain.getHighMaintainTime()),
                "maintain_time", maintain.getHighMaintainTime());
        queryWrapper.ge(ObjectUtil.isNotEmpty(maintain.getLowMaintainTime()),
                "maintain_time", maintain.getLowMaintainTime());
        queryWrapper.le(ObjectUtil.isNotEmpty(maintain.getHighCost()),
                "cost", maintain.getHighCost());
        queryWrapper.ge(ObjectUtil.isNotEmpty(maintain.getLowCost()),
                "cost", maintain.getLowCost());
        queryWrapper.orderByDesc("maintain_time");
        return maintainMapper.selectPage(page, queryWrapper);
    }


    /**
     * 删除及批量删除
     *
     * @param ids
     * @return
     */
    @Override
    public boolean delete(String ids) {
        List<Integer> list = Arrays.stream(ids.split(",")).map(Integer::parseInt).toList();
        if (!list.isEmpty()) {
            list.forEach(id -> {
                Maintain maintain = baseMapper.selectById(id);
                if (ObjectUtil.isNotEmpty(maintain)) {
                    Integer carrentalId = maintain.getCarrentalId();
                    CarrentalInfo carrentalInfo = autoInfoMapper.selectById(carrentalId);
                    carrentalInfo.setActualNum(carrentalInfo.getActualNum() - 1);
                    autoInfoMapper.updateById(carrentalInfo);
                }
            });
            return baseMapper.deleteBatchIds(list) > 0;
        }
        return false;
    }
}


